#pragma once

#include <algorithm>
#include <array>
#include <atomic>
#include <cstddef>

namespace mp {

constexpr size_t ALIGNMENT = 8;           // 等同于指针大小
constexpr size_t MAX_BYTES = 256 * 1024;  // 256 KB
constexpr size_t FREE_LIST_SIZE = MAX_BYTES / ALIGNMENT;

/* 内存块头部信息 */
struct BlockHeader {
    size_t size;  // 内存块大小
    bool inUse;   // 使用标志
    BlockHeader *next;
};

/* 大小类管理 */
class SizeClass {
  public:
    static size_t roundUp(size_t bytes) { return (bytes + ALIGNMENT - 1) & ~(ALIGNMENT - 1); }

    static size_t getIdx(size_t bytes) {
        bytes = std::max(bytes, ALIGNMENT);
        return (bytes + ALIGNMENT - 1) / ALIGNMENT - 1;  //? 向上取整后 -1
    }
};

}  // namespace MemoryPool